package com.seanzx.resolver.impl;

import com.seanzx.common.Result;
import com.seanzx.constant.SymbolTable;
import com.seanzx.lexical.Token;
import com.seanzx.lexical.TokenType;
import com.seanzx.resolver.IResolver;
import com.seanzx.dp.ProcessorController;
import com.seanzx.dp.entity.DataInsertEntity;

import java.util.ArrayList;
import java.util.List;

public class InsertDataResolver implements IResolver<DataInsertEntity> {
    private final ProcessorController processorController = ProcessorController.get();

    @Override
    public boolean matches(List<Token> tokens) {
        return tokens.size() > 3 && SymbolTable.INSERT.equals(tokens.get(0).getValue()) && "INTO".equals(tokens.get(1).getValue());
    }

    @Override
    public DataInsertEntity translate(List<Token> tokens) {
        DataInsertEntity entity = new DataInsertEntity();

        int startIndex;
        if (tokens.get(3).getValue().equals(".")) {
            entity.setDatabaseName(tokens.get(2).getValue());
            entity.setTableName(tokens.get(4).getValue());
            startIndex = 5;
        } else {
            entity.setTableName(tokens.get(2).getValue());
            startIndex = 3;
        }
        if ("VALUES".equals(tokens.get(startIndex).getValue())) {
            List<List<String>> rows = new ArrayList<>();
            List<String> row = new ArrayList<>();
            while (startIndex++ < tokens.size() - 1) {
                if (")".equals(tokens.get(startIndex).getValue())) {
                    rows.add(row);
                    row = new ArrayList<>();
                }
                if (!TokenType.VALUE.equals(tokens.get(startIndex).getType())) {
                    continue;
                }
                row.add(tokens.get(startIndex).getValue());
            }
            entity.setRows(rows);
        } else {
            entity.setColumns(new ArrayList<>());
            while (startIndex++ < tokens.size() - 1) {
                if (")".equals(tokens.get(startIndex).getValue())) {
                    startIndex++;
                    break;
                }
                if (!TokenType.VALUE.equals(tokens.get(startIndex).getType())) {
                    continue;
                }
                entity.getColumns().add(tokens.get(startIndex).getValue());
            }
            List<List<String>> rows = new ArrayList<>();
            List<String> row = new ArrayList<>();
            while (startIndex++ < tokens.size()) {
                if (")".equals(tokens.get(startIndex).getValue())) {
                    rows.add(row);
                    row = new ArrayList<>();
                }
                if (!TokenType.VALUE.equals(tokens.get(startIndex).getType())) {
                    continue;
                }
                row.add(tokens.get(startIndex).getValue());
            }
            entity.setRows(rows);
        }
        return entity;
    }

    @Override
    public boolean validate(Object parameter) {
        return true;
    }

    @Override
    public Result access(Object parameter) {
        return processorController.insert((DataInsertEntity) parameter);
    }

}
